﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BalanceCenter_Score : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            long id = ShoveEL._Convert.StrToLong(this.Request["id"], -1);

            if ((_User.Competences[Competences.Administrator]) && (id >= 0))
            {
                tbID.Text = id.ToString();
            }
            else
            {
                tbID.Text = _User.ID.ToString();
            }

            BindData();     //业绩
            BindData2();    //业绩明细
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = "BalanceCenter/Score.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        long ID = ShoveEL._Convert.StrToLong(tbID.Text, -1);
        if (ID < 0)
        {
            ID = _User.ID;
        }

        if (tbData.Text.Trim() != "")
        {
            User tu = new User();
            string Return = "";
            int Result = 0;

            if (ddlType.SelectedValue == "Name")
            {
                tu.Name = tbData.Text.Trim();
                Result = tu.GetUserInfoByName(ref Return);
            }
            else
            {
                tu.RealityName = tbData.Text.Trim();
                Result = tu.GetUserInfoByRealityName(ref Return);
            }

            ID = tu.ID;
        }

        //读总业绩
        double L = 0;
        double R = 0;
        double L_CurrentMonth = 0;
        double R_CurrentMonth = 0;
        double L_CurrentDay = 0;
        double R_CurrentDay = 0;
        double L_Residual = 0;
        double R_Residual = 0;

        if (DAL.Procedures.P_GetScoreForUser.Call(ID, ref L, ref R, ref L_CurrentMonth, ref R_CurrentMonth, ref L_CurrentDay, ref R_CurrentDay, ref L_Residual, ref R_Residual) < 0)
        {
            labSL1.Text = "0";
            labSR1.Text = "0";
            labSL2.Text = "0";
            labSR2.Text = "0";
            labSL3.Text = "0";
            labSR3.Text = "0";
            labSL4.Text = "0";
            labSR4.Text = "0";
        }
        else
        {
            labSL1.Text = L.ToString();
            labSR1.Text = R.ToString();
            labSL2.Text = L_CurrentMonth.ToString();
            labSR2.Text = R_CurrentMonth.ToString();
            labSL3.Text = L_CurrentDay.ToString();
            labSR3.Text = R_CurrentDay.ToString();
            labSL4.Text = L_Residual.ToString();
            labSR4.Text = R_Residual.ToString();
        }

        labSumDistilled.Text = DAL.Functions.F_GetUserBumpedSum.Call(ID).ToString(); 

        DataTable dt = DAL.Tables.T_UsersSubsOfDay.Select("isnull(sum(LDistilled), 0), isnull(sum(RDistilled), 0)", "UserID = " + ID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        int X = int.Parse(DAL.Functions.F_GetOptions.Call("1vsX"));

        if (dt.Rows.Count < 1)
        {
            labSumDistilled.Text = "0";
        }
        else
        {
            int SumDistilled = int.Parse(dt.Rows[0][0].ToString()) + int.Parse(dt.Rows[0][1].ToString());
            SumDistilled = SumDistilled / (1 + X);
            labSumDistilled.Text = SumDistilled.ToString();
        }

        labSumDistilled2.Text = labSumDistilled.Text;
    }

    private void BindData2()
    {
        DateTime StartTime = DateTime.Today;
        DateTime EndTime = DateTime.Today.AddDays(1);

        if (tbStartTime.Value != "" && tbStartTime.Value != null && tbEndTime.Value != "" && tbEndTime.Value != null)
        {
            StartTime = ShoveEL._Convert.StrToDateTime(tbStartTime.Value, "");
            EndTime = ShoveEL._Convert.StrToDateTime(tbEndTime.Value, "");
        }

        long UserID = _User.ID;

        UserID = ShoveEL._Convert.StrToLong(tbID.Text, -1);
        if (UserID < 0)
        {
            UserID = _User.ID;
        }

        if (tbData.Text.Trim() != "")
        {
            User tu = new User();
            string Return = "";
            int Result = 0;

            if (ddlType.SelectedValue == "Name")
            {
                tu.Name = tbData.Text.Trim();
                Result = tu.GetUserInfoByName(ref Return);
            }
            else
            {
                tu.RealityName = tbData.Text.Trim();
                Result = tu.GetUserInfoByRealityName(ref Return);
            }

            UserID = tu.ID;
        }

        string ReturnDescription = "";
        DataSet ds = null;

        ds = DAL.Procedures.P_GetNetForUser.Open(UserID, StartTime, EndTime, ref ReturnDescription);

        if (ReturnDescription != "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, ReturnDescription);
            return;
        }

        if (ds.Tables.Count > 0 && ds != null)
        {
            DataTable dt = ds.Tables[0];

            g2.DataSource = dt;
            g2.DataBind();
            gPager.Enabled = dt.Rows.Count > 0;
        }
       
    }

    protected void g2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            //for (int i = 3; i <= 10; i++)
            //{
            //    double money = ShoveEL._Convert.StrToDouble(e.Item.Cells[i].Text, 0);
            //    e.Item.Cells[i].Text = (money == 0) ? "" : money.ToString("N");
            //}

            //double money3 = ShoveEL._Convert.StrToDouble(e.Item.Cells[3].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[4].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[5].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[6].Text, 0);
            //double money4 = ShoveEL._Convert.StrToDouble(e.Item.Cells[7].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[8].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[9].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[10].Text, 0);
            //e.Item.Cells[11].Text = (money3 - money4).ToString("N");

            //e.Item.Attributes["onmouseover"] = "this.name=this.style.backgroundColor;this.style.backgroundColor='MistyRose'";
            //e.Item.Attributes["onmouseout"] = "this.style.backgroundColor=this.name;";
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData2();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData2();
    }

    protected void ibSelect_Click(object sender, ImageClickEventArgs e)
    {
        //只允许查询自己的下线会员的业绩
        if (tbData.Text.Trim() != "")
        {
            User tu = new User();
            string Return = "";
            int Result = 0;

            if (ddlType.SelectedValue == "Name")
            {
                tu.Name = tbData.Text.Trim();
                Result = tu.GetUserInfoByName(ref Return);
            }
            else
            {
                tu.RealityName = tbData.Text.Trim();
                Result = tu.GetUserInfoByRealityName(ref Return);
            }

            if (tu.ID > 0)
            {
                DataTable dt = DAL.Tables.T_Users.Select("top 1 *", tu.ID + " in(select ID from F_GetUserNet(" + _User.ID + "))", "");
                //string sql = "select top 1 * from T_Users where " + tu.ID + " in(select ID from F_GetUserNet(" + _User.ID + "))";

                if ((dt == null) || (dt.Rows.Count == 0))
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "该会员不是您的下线");
                    return;
                }
            }
        }        

        BindData();
        BindData2();
    }
}